﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace monitor_2_DBwork
{
    public class LicHelper
    {
        public static bool CompareByteArrays(IEnumerable<byte> arrayA, IList<byte> arrayB)
        {
            if (arrayA == null || arrayB == null)
                return false;
            return !arrayA.Where((t, i) => arrayB[i] != t).Any();
        }

        public static byte[] GetHash(string input)
        {

            // Convert the input string to a byte array and compute the hash.
            var md5Hash = SHA1.Create();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            return data;

            //// Create a new Stringbuilder to collect the bytes
            //// and create a string.
            //var sBuilder = new StringBuilder();

            //// Loop through each byte of the hashed data 
            //// and format each one as a hexadecimal string.
            //for (int i = 0; i < data.Length; i++)
            //{
            //    sBuilder.Append(data[i].ToString("x2"));
            //}

            //// Return the hexadecimal string.
            //return sBuilder.ToString();
        }

        public static byte[] GetHash(byte[] input)
        {
            var md5Hash = SHA1.Create();
            byte[] data = md5Hash.ComputeHash(input);

            return data;
        }

        public static byte[] Encrypt(byte[] toEncrypt, byte[] key)
        {
            Aes aes = new AesCryptoServiceProvider();
            var enc = aes.CreateEncryptor(key, key);
            var a = enc.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
            return a;
        }

        public static byte[] Decrypt(byte[] toDecrypt, byte[] key)
        {
            Aes aes = new AesCryptoServiceProvider();
            var dec = aes.CreateDecryptor(key, key);
            byte[] a = dec.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length);

            return a;
        }
    }
}
